package io.github.maojian.bianrysearch;

import org.junit.Test;

/**
 * @Auther: jian.mao
 * @Date: 2022/5/20 19:37
 * @Description: https://leetcode.cn/problems/sqrtx/
 */
public class Q69 {

    //二分查找
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int mid;
        int ans = -1;
        while (left <= right) {
            mid = (left + right) / 2;
            if ((long)mid * mid <= x) {
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return ans;
    }

    @Test
    public void test(){
        System.out.println(mySqrt(81));
    }
}
